home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / AVLMap.hP < prev    next >
Text File  |  1992-05-29  |  3KB  |  142 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T><C>AVLMap_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T><C>AVLMap_h 1
  25.  
  26. #include "<T>.<C>.Map.h"
  27.  
  28. struct <T><C>AVLNode
  29. {
  30.   <T><C>AVLNode*      lt;
  31.   <T><C>AVLNode*      rt;
  32.   <T>                 item;
  33.   <C>                 cont;
  34.   char                stat;
  35.                       <T><C>AVLNode(<T&> h, <C&> c, 
  36.                                     <T><C>AVLNode* l=0, <T><C>AVLNode* r=0);
  37.                       ~<T><C>AVLNode();
  38. };
  39.  
  40. inline <T><C>AVLNode::<T><C>AVLNode(<T&> h, <C&> c, 
  41.                                     <T><C>AVLNode* l, <T><C>AVLNode* r)
  42.      :item(h), cont(c), lt(l), rt(r), stat(0) {}
  43.  
  44. inline <T><C>AVLNode::~<T><C>AVLNode() {}
  45.  
  46. typedef <T><C>AVLNode* <T><C>AVLNodePtr;
  47.  
  48.  
  49. class <T><C>AVLMap : public <T><C>Map
  50. {
  51. protected:
  52.   <T><C>AVLNode*   root;
  53.  
  54.   <T><C>AVLNode*   leftmost();
  55.   <T><C>AVLNode*   rightmost();
  56.   <T><C>AVLNode*   pred(<T><C>AVLNode* t);
  57.   <T><C>AVLNode*   succ(<T><C>AVLNode* t);
  58.   void            _kill(<T><C>AVLNode* t);
  59.   void            _add(<T><C>AVLNode*& t);
  60.   void            _del(<T><C>AVLNode* p, <T><C>AVLNode*& t);
  61.  
  62. public:
  63.                 <T><C>AVLMap(<C&> dflt);
  64.                 <T><C>AVLMap(<T><C>AVLMap& a);
  65.                 ~<T><C>AVLMap();
  66.  
  67.   <C>&          operator [] (<T&> key);
  68.  
  69.   void          del(<T&> key);
  70.  
  71.   Pix           first();
  72.   void          next(Pix& i);
  73.   <T>&          key(Pix i);
  74.   <C>&          contents(Pix i);
  75.  
  76.   Pix           seek(<T&> key);
  77.   int           contains(<T&> key);
  78.  
  79.   void          clear(); 
  80.  
  81.   Pix           last();
  82.   void          prev(Pix& i);
  83.  
  84.   int           OK();
  85. };
  86.  
  87. inline <T><C>AVLMap::~<T><C>AVLMap()
  88. {
  89.   _kill(root);
  90. }
  91.  
  92. inline <T><C>AVLMap::<T><C>AVLMap(<C&> dflt) :<T><C>Map(dflt)
  93. {
  94.   root = 0;
  95. }
  96.  
  97. inline Pix <T><C>AVLMap::first()
  98. {
  99.   return Pix(leftmost());
  100. }
  101.  
  102. inline Pix <T><C>AVLMap::last()
  103. {
  104.   return Pix(rightmost());
  105. }
  106.  
  107. inline void <T><C>AVLMap::next(Pix& i)
  108. {
  109.   if (i != 0) i = Pix(succ((<T><C>AVLNode*)i));
  110. }
  111.  
  112. inline void <T><C>AVLMap::prev(Pix& i)
  113. {
  114.   if (i != 0) i = Pix(pred((<T><C>AVLNode*)i));
  115. }
  116.  
  117. inline <T>& <T><C>AVLMap::key(Pix i)
  118. {
  119.   if (i == 0) error("null Pix");
  120.   return ((<T><C>AVLNode*)i)->item;
  121. }
  122.  
  123. inline <C>& <T><C>AVLMap::contents(Pix i)
  124. {
  125.   if (i == 0) error("null Pix");
  126.   return ((<T><C>AVLNode*)i)->cont;
  127. }
  128.  
  129. inline void <T><C>AVLMap::clear()
  130. {
  131.   _kill(root);
  132.   count = 0;
  133.   root = 0;
  134. }
  135.  
  136. inline int <T><C>AVLMap::contains(<T&> key)
  137. {
  138.   return seek(key) != 0;
  139. }
  140.  
  141. #endif
  142.